<?php
/**
*
* @author Blackpawn blackpawn666@hotmail.com - http://blackpawnsarchives.net
* @package phpbb3 :: Game Console Mod
* @version $Id: acp_gameconsole.php 1.0.2 12-10-2009 Blackpawn $
* @copyright (c) 2004-2009 Blackpawns Archives
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

// Report all errors, except notices
error_reporting(E_ALL ^ E_NOTICE);
 
/**
* @package acp_gameconsole
*/
class acp_gameconsole
{
	var $u_action;
   
	function main($id, $mode)
	{
		define('GC_MOD',true);
		global $db, $user, $auth, $template;
		global $config, $game_config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
		
		include($phpbb_root_path . 'includes/mods/game_console/gc_common.' . $phpEx);
		
		$form_key = 'acp_gameconsole';
		add_form_key($form_key);
		
		switch($mode)
		{
			case 'update':
				// Variables
				$current_version = null;
				$errstr = null;
				$errno = 0;
				$info = null;
				$latest_version = null;
				$update_url = null;
				$update_link = null;
				
				if (!$auth->acl_get('a_gc_u_config'))
				{
					trigger_error('UNAUTHORIZED_ACCESS', E_USER_WARNING);
				}

				$this->page_title = 'GAME_CONSOLE_SETTINGS';
				$this->tpl_name = 'acp_gc_update';
				
				$current_version = $config['gc_version_major'].'.'.$config['gc_version_minor'].'.'.$config['gc_version_build'];
		
				$info = get_remote_file('www.blackpawnsarchives.net', '/software/fw/ver/', 'gc.txt' , $errstr, $errno);
		
				if ($info === false)
				{
					trigger_error($errstr, E_USER_WARNING);
				}
		
				$info = explode("\n", $info);
				$latest_version = trim($info[0]);

				$update_url = trim($info[1]);


				if(isset($update_url)){
					$update_link = sprintf($user->lang['GAME_CONSOLE_UPDATE_LINK'],$update_url);
				}else{
					$update_link = sprintf($user->lang['GAME_CONSOLE_UPDATE_LINK'],$game_config['support_link']);
				}

				
				$template->assign_vars(array(
					'LATEST_VERSION'	=> $latest_version,
					'CURRENT_VERSION'	=> $current_version,
					'UPDATE_LINK'		=> $update_link,
					)
				);
			break;
						
			case 'settings':
				// Variables
				$submit = null;

				if (!$auth->acl_get('a_gc_set_config'))
				{
					trigger_error('UNAUTHORIZED_ACCESS', E_USER_WARNING);
				}
								
				$submit = (isset($_POST['submit'])) ? true : false;

				$this->page_title = 'GAME_CONSOLE_SETTINGS';
				$this->tpl_name = 'acp_gc_settings';
				
				$gc_config_row = array(
				'game_console_online' 	=> request_var('game_console_online', 0),				
				'games_per_page' 	=> request_var('games_per_page', 5),
				);
				
				if ($submit)
				{
					if (!check_form_key('acp_gameconsole'))
					{
						trigger_error('FORM_INVALID');
					}
					
					foreach ($gc_config_row as $config_name => $config_value)
					{
						set_game_config($config_name, $config_value);
					}
					
					add_log('admin', 'GAME_CONSOLE_LOG_UPDATE');
					trigger_error($user->lang['GAME_CONSOLE_LOG_UPDATE'] . adm_back_link($this->u_action));
				}								


				$template->assign_vars(array(
					'U_ACTION'				=> $this->u_action,
					'GAME_CONSOLE_ENABLE'	=> $game_config['game_console_online'],
					'S_GAMES_PER_PAGE'		=> $game_config['games_per_page'],
					)
				);
				
			break;
			
			case 'style':
				// Variables
				$submit = null;
								
				if (!$auth->acl_get('a_gc_sty_config'))
				{
					trigger_error('UNAUTHORIZED_ACCESS', E_USER_WARNING);
				}	
							
				$submit = (isset($_POST['submit'])) ? true : false;
				
				$this->page_title = 'GAME_CONSOLE_STYLE';
				$this->tpl_name = 'acp_gc_style';

				$gc_config_row = array(
					'text_color' 						=> request_var('text_color', '000000'),
					'gc_panel_bgcolor'					=> request_var('gc_panel_bgcolor', '000000'),
					'link_text_color' 					=> request_var('link_text_color', '000000'),
					'visited_text_color' 				=> request_var('visited_text_color', '000000'),
					'hover_text_color' 					=> request_var('hover_text_color', '000000'),
					'active_text_color' 				=> request_var('active_text_color', '000000'),
					'headerbar_bg_color' 				=> request_var('headerbar_bg_color', '000000'),
					'headerbar_text_color' 				=> request_var('headerbar_text_color', '000000'),
					'gc_table_border_color' 			=> request_var('gc_table_border_color', '000000'),
					'gc_row_1' 							=> request_var('gc_row_1', '000000'),
					'gc_row_2' 							=> request_var('gc_row_2', '000000'),
					'gc_row_3' 							=> request_var('gc_row_3', '000000'),
					'headerbar_bg_color' 				=> request_var('headerbar_bg_color', '000000'),
					'headerbar_text_color' 				=> request_var('headerbar_text_color', '000000'),
					'announcementbar_bg_color' 			=> request_var('announcementbar_bg_color', '000000'),
					'announcementbar_text_color' 		=> request_var('announcementbar_text_color', '000000'),
				
				);

				if ($submit)
				{
					if (!check_form_key('acp_gameconsole'))
					{
						trigger_error('FORM_INVALID');
					}
					
					foreach ($gc_config_row as $config_name => $config_value)
					{
						set_game_config($config_name, $config_value);
					}
					
					add_log('admin', 'GAME_CONSOLE_STYLE_LOG_UPDATE');
					trigger_error($user->lang['GAME_CONSOLE_STYLE_LOG_UPDATE'] . adm_back_link($this->u_action));
				}	

				$template->assign_vars(array(
					'TEXT_COLOR'					=>	$game_config['text_color'],
					'PANEL_COLOR'					=>	$game_config['gc_panel_bgcolor'],
					'LINK_TEXT_COLOR'				=>	$game_config['link_text_color'],
					'VISITED_LINK_TEXT_COLOR'		=>	$game_config['visited_text_color'],
					'HOVER_LINK_TEXT_COLOR'			=>	$game_config['hover_text_color'],
					'ACTIVE_LINK_TEXT_COLOR'		=>	$game_config['active_text_color'],
					'TABLE_BORDER_COLOR'			=>	$game_config['gc_table_border_color'],
					'TABLE_ROW1_COLOR'				=>	$game_config['gc_row_1'],
					'TABLE_ROW2_COLOR'				=>	$game_config['gc_row_2'],
					'TABLE_ROW3_COLOR'				=>	$game_config['gc_row_3'],
					'HEADER_BG_COLOR'				=>	$game_config['headerbar_bg_color'],
					'HEADER_FG_COLOR'				=>	$game_config['headerbar_text_color'],
					'TICKER_BG_COLOR'				=>	$game_config['announcementbar_bg_color'],
					'TICKER_FG_COLOR'				=>	$game_config['announcementbar_text_color'],

					)
				);
			break;

			case 'add':
				// Variables
				$query_code = Null;
				$error_msgs = Null;
				$game_member_slots = Null;
				$game_ticks_left = Null;
				$game_round = Null;
				$game_round_length = Null;
				$game_bulletin = Null;
				$tab = Null;
				$gameselected = Null;
				$formdata = Null;
				$game_name = Null;
				$game_desc = Null;
				$game_ver_major = Null;
				$game_ver_minor = Null;
				$game_ver_build = Null;
				$game_path  = Null;
				$game_author = Null;
				$game_email = Null;
				$game_website = Null;
				$game_credits = Null;
				$game_history = Null;
				$game_license = Null;
				$game_copyrights = Null;
				$game_sql = Null;
				$game_tables = Null;
				$game_online = Null;
				$game_open = Null;
				$game_time_mgt = Null;
				$game_bulletin_show = Null;
				$game_unique_id = Null;
												 
				if (!$auth->acl_get('a_gc_a_config'))
					{
					trigger_error('UNAUTHORIZED_ACCESS', E_USER_WARNING);
				}
				
				$add = (isset($_POST['add'])) ? true : false;
				$gameid = request_var('gameid', 0);
							
				$filename = $phpbb_root_path.'games/install.' . $phpEx;

				if (file_exists($filename)) {
					include($filename);
					$newgame = TRUE;
					
					// Build a unique game id with the game install data
					$game_unique_id = md5($game_name). '-' .md5($game_desc). '-' .md5($game_ver_major). '-' .md5($game_ver_minor). '-' .md5($game_ver_build). '-' .md5($game_path). '-' .md5($game_author). '-' .md5($game_email). '-' .md5($game_website). '-' .md5($game_credits). '-' .md5($game_history). '-' .md5($game_license). '-' .md5($game_copyrights). '-' .md5($game_tables);

					// Check to see if a game with the unique game id exists
					$game_unique_id_check = gc_check_game_unique_id($game_unique_id);

					// If game already exists then prevent from reinstalling
					if($game_unique_id_check)
					{
						$newgame = FALSE;
						$error_msgs .= '<div align="center" style="border:solid thin #000000; background:#FF0000;"><font color="#FFFFFF"><b><br />.:'.$user->lang['GAME_CONSOLE_IMPORTANT_NOTICE'].':.<br />'.$user->lang['GAME_CONSOLE_REMOVE_INSTALL'].'<br /><br /></b></font></div>';
					}
				}
				
				if($add)
				{

					$name_check = gc_check_game_name($game_name);
					
					if($name_check == TRUE)
					{
						trigger_error($user->lang['GAME_NAME_ALREADY_USED'] . adm_back_link($this->u_action) , E_USER_WARNING);
					}					
					
					$path_check = gc_check_game_path($game_path);
					
					if($path_check == TRUE)
					{
						trigger_error($user->lang['GAME_PATH_ALREADY_USED'] . adm_back_link($this->u_action) , E_USER_WARNING);
					}					
					
					// Build a unique game id with the game install data
					$game_unique_id = md5($game_name). '-' .md5($game_desc). '-' .md5($game_ver_major). '-' .md5($game_ver_minor). '-' .md5($game_ver_build). '-' .md5($game_path). '-' .md5($game_author). '-' .md5($game_email). '-' .md5($game_website). '-' .md5($game_credits). '-' .md5($game_history). '-' .md5($game_license). '-' .md5($game_copyrights). '-' .md5($game_tables);

					// Check to see if a game with the unique game id exists
					$game_unique_id_check = gc_check_game_unique_id($game_unique_id);

					// If game already exists then prevent from reinstalling
					if($game_unique_id_check)
					{
						trigger_error($user->lang['GAME_CONSOLE_ALREADY_INSTALLED'] . adm_back_link($this->u_action) , E_USER_WARNING);

					}
					else
					{
						
						$sql = 'INSERT INTO ' . GC_GAMES_TABLE .' ' . $db->sql_build_array('INSERT', array(
							'game_name' 		=> $game_name,
							'game_descript' 	=> $game_desc,
							'game_ver_major' 	=> $game_ver_major,
							'game_ver_minor' 	=> $game_ver_minor,
							'game_ver_build' 	=> $game_ver_build,
							'game_path' 		=> $game_path, 
							'game_author' 		=> $game_author,
							'game_email' 		=> $game_email,
							'game_website' 		=> $game_website,
							'game_credits' 		=> $game_credits,
							'game_history' 		=> $game_history,
							'game_license' 		=> $game_license,
							'game_copyrights' 	=> $game_copyrights,
							'game_tables' 		=> $game_tables,
							'game_unique_id'	=> $game_unique_id,
						));				  
						$db->sql_query($sql);
						
												
						$queries = count($game_sql);
				
						for($i=0; $i < $queries; ++$i)
						{
							$db->sql_query($game_sql[$i]);					
						}
											
												
						add_log('admin', 'GAME_CONSOLE_GAME_ADD_LOG_UPDATE');
						trigger_error($user->lang['GAME_CONSOLE_GAME_ADD_LOG_UPDATE'] . adm_back_link($this->u_action));					
					}
				}
				$queries = count($game_sql);
				
				for($i=0; $i < $queries; ++$i)
				{
					$code = str_replace("\n", "<br />", $game_sql[$i]);
					$query_code = $query_code . $code.'<br />';		
				}
				
				$this->page_title = 'GAME_CONSOLE_ADD';
				$this->tpl_name = 'acp_gc_add';
				
				$template->assign_vars(array(
					'U_ACTION'				=> $this->u_action,
					'ERROR_MSGS'			=> $error_msgs,

					'GAME_ID'				=> $gameid,
					'GAME_NAME'				=> $game_name,
					'GAME_DESC'				=> $game_desc,
					'GAME_VER_MAJOR'		=> $game_ver_major,
					'GAME_VER_MINOR'		=> $game_ver_minor,
					'GAME_VER_BUILD'		=> $game_ver_build,
					'GAME_PATH'				=> $game_path,
					'GAME_AUTHOR'			=> $game_author,
					'GAME_EMAIL'			=> $game_email,
					'GAME_WEBSITE'			=> $game_website,
					'GAME_CREDITS'			=> $game_credits,
					'GAME_HISTORY'			=> $game_history,
					'GAME_LICENSE'			=> $game_license,
					'GAME_COPYRIGHTS'		=> $game_copyrights,
					'GAME_QUERIES'			=> count($game_sql).' Queries<br / >'.$query_code,

					'GAME_MEMBER_SLOTS'		=> $game_member_slots,
					'GAME_TICKS_LEFT'		=> $game_ticks_left,
					'GAME_ROUND'			=> $game_round,
					'GAME_ROUND_LENGTH'		=> $game_round_length,
					'GAME_BULLETIN'			=> $game_bulletin,
						
					'S_NEW_GAME'			=> $newgame,
					'S_TAB' 				=> $tab,
					'S_GAME_SELECTED'		=> $gameselected,
					'S_FORM_DATA'			=> $formdata,
					'S_GAME_ONLINE'			=> $game_online,
					'S_GAME_OPEN'			=> $game_open,
					'S_GAME_TIME_MGT' 		=> $game_time_mgt,
					'S_BULLETIN_SHOW'		=> $game_bulletin_show,
									
					)
				);				
					break;
					
			case 'delete':
				if (!$auth->acl_get('a_gc_d_config'))
				{
					trigger_error('UNAUTHORIZED_ACCESS', E_USER_WARNING);
				}	
				$remove = (isset($_POST['remove'])) ? true : false;
				$delete = (isset($_POST['delete'])) ? true : false;
				$gameid = request_var('gameid', 0);

				if($delete)
				{
					// Check confirm box results
					if (confirm_box(true))
					{
						if(!$gameid)
						{
							trigger_error($user->lang['GAME_CONSOLE_ERROR_BAD_GAME_ID'] . adm_back_link($this->u_action) , E_USER_WARNING);
						}	
											
						// Remove the selected game from the game console
						gc_remove_game($gameid);
						
						add_log('admin', 'GAME_CONSOLE_GAME_REMOVE_LOG_UPDATE');
						trigger_error($user->lang['GAME_CONSOLE_GAME_REMOVE_LOG_UPDATE'] . adm_back_link($this->u_action));											
					}
					else
					{
						//redirect($this->u_action);
						redirect(append_sid($this->u_action));		
					}					
				}

				if ($remove)
				{					
					if(!$gameid)
					{
						trigger_error($user->lang['GAME_CONSOLE_ERROR_BAD_GAME_ID'] . adm_back_link($this->u_action) , E_USER_WARNING);
					}

					$s_hidden_fields = build_hidden_fields(array(
						'delete'    => true,
						'gameid' => $gameid,
						)
					);

					//Display confirm box
					confirm_box(false, $user->lang['GAME_CONSOLE_CONFIRM_GAME_DELETE'], $s_hidden_fields);
					
				}
								
				$this->page_title = 'GAME_CONSOLE_DELETE';
				$this->tpl_name = 'acp_gc_delete';

				$template->assign_vars(array(
					'GAME_OPTIONS' => gc_fetch_game_option_list(),
					)
				);				
					break;

			case 'config':
				// Variables
				$submit = Null;
				$select = Null;
				$update = Null;
				$reset = Null;
				$query_code = Null;
				$error_msgs = Null;
				$formdata = Null;
				$tab = Null;
				$newgame = Null;
				$game = Null;
				$gameselected = Null;
				$gameid = Null;
				$game_time_mgt = Null;
				$game_round_length = Null; 
				$game_member_slots = Null;
				$game_ticks_left = Null;
				$game_round = Null;
				$game_round_length = Null;	
				$game_name = Null;
				$game_desc = Null;
				$game_ver_major = Null;
				$game_ver_minor = Null;
				$game_ver_build = Null;
				$game_path  = Null;
				$game_author = Null;
				$game_email = Null;
				$game_website = Null;
				$game_credits = Null;
				$game_history = Null;
				$game_license = Null;
				$game_copyrights = Null;
				$game_sql = Null;
				$game_tables = Null;
				$game_online = Null;
				$game_open = Null;
				$game_time_mgt = Null;
				$game_bulletin = Null;
				$game_bulletin_show = Null;
				$game_unique_id = Null;
				$new_ticks_left = Null;
				$sql_ary = Null;
				
				
				if (!$auth->acl_get('a_gc_m_config'))
				{
					trigger_error('UNAUTHORIZED_ACCESS', E_USER_WARNING);
				}	

				$submit = (isset($_POST['submit'])) ? true : false;
				$select = (isset($_POST['select'])) ? true : false;
				$update = (isset($_POST['update'])) ? true : false;
				$reset = (isset($_POST['reset'])) ? true : false;
				
				$gameid = request_var('gameid', 0);

				if ($reset)
				{					
					if(!$gameid)
					{
						trigger_error($user->lang['GAME_CONSOLE_ERROR_BAD_GAME_ID'] . adm_back_link($this->u_action) , E_USER_WARNING);
					}
					
					$game 				= gc_fetch_game_data($gameid);
					$game_time_mgt 		= $game['time_mgt'];
					$game_round_length 	= $game['round_length']; 

					if($game_time_mgt < 1)
					{
						trigger_error($user->lang['GAME_CONSOLE_ERROR_TIME_MGT_NOT_SET'] . adm_back_link($this->u_action) , E_USER_WARNING);
					}
					
					if($game_round_length < 1)
					{
						trigger_error($user->lang['GAME_CONSOLE_ERROR_ROUND_LENGTH_NOT_SET'] . adm_back_link($this->u_action) , E_USER_WARNING);
					}					
					
					$new_ticks_left = ((floor(60 / $game_time_mgt) * 24) * $game_round_length);
										
					$sql_ary = array(
						'game_ticks_left'		=> $new_ticks_left,
					);

					if (!check_form_key('acp_gameconsole'))
					{
						trigger_error('FORM_INVALID');
					}
					
					$db->sql_query('UPDATE ' . GC_GAMES_TABLE .' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . " WHERE id = $gameid");
					$db->sql_return_on_error(false);					

					trigger_error($user->lang['GAME_CONSOLE_TICKS_LEFT_UPDATED'] . adm_back_link($this->u_action));					

				}
				
				if ($update)
				{					
					if(!$gameid)
					{
						trigger_error($user->lang['GAME_CONSOLE_ERROR_BAD_GAME_ID'] . adm_back_link($this->u_action) , E_USER_WARNING);
					}
					
					$sql_ary = array(
						'game_online' 			=> request_var('game_online', 0),
						'game_open' 			=> request_var('game_open', 0),
						'game_member_slots' 	=> request_var('game_member_slots', 0),
						'game_time_mgt' 		=> request_var('game_time_mgt', 5),
						'game_ticks_left'		=> request_var('game_ticks_left', 0),
						'game_round'			=> request_var('game_round', 0),
						'game_round_length'		=> request_var('game_round_length', 0),
						'game_bulletin'			=> request_var('game_bulletin', ''),
						'game_bulletin_show'	=> request_var('game_bulletin_show', 0),
					);

					if (!check_form_key('acp_gameconsole'))
					{
						trigger_error('FORM_INVALID');
					}
					
					$db->sql_query('UPDATE ' . GC_GAMES_TABLE .' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . " WHERE id = $gameid");
					$db->sql_return_on_error(false);					


					add_log('admin', 'GAME_CONSOLE_LOG_GAME_UPDATE');
					trigger_error($user->lang['GAME_CONSOLE_LOG_GAME_UPDATE'] . adm_back_link($this->u_action));
				}
								
				$this->page_title = 'GAME_CONSOLE_MANAGE';
				$this->tpl_name = 'acp_gc_manage';

				//if($gameid && $select == true)
				if($gameid)
				{
					
					$game 				= gc_fetch_game_data($gameid);
					$game_name 			= $game['name'];
					$game_online 		= $game['online'];
					$game_open 			= $game['open'];
					$game_member_slots 	= $game['member_slots'];
					$game_time_mgt 		= $game['time_mgt'];
					$game_ticks_left 	= $game['ticks_left'];
					$game_round 		= $game['round'];
					$game_round_length 	= $game['round_length'];
					$game_bulletin 		= $game['bulletin'];
					$game_bulletin_show = $game['bulletin_show'];

				}
				
				if($gameid)
				{
					$gameselected=true;
				}else{
					$gameselected=false;
				}
				
				$template->assign_vars(array(
					'GAME_OPTIONS'			=> gc_fetch_game_option_list(),

					'GAME_ID'				=> $gameid,
					'GAME_NAME'				=> $game_name,
					'GAME_DESC'				=> $game_desc,
					'GAME_VER_MAJOR'		=> $game_ver_major,
					'GAME_VER_MINOR'		=> $game_ver_minor,
					'GAME_VER_BUILD'		=> $game_ver_build,
					'GAME_PATH'				=> $game_path,
					'GAME_AUTHOR'			=> $game_author,
					'GAME_EMAIL'			=> $game_email,
					'GAME_WEBSITE'			=> $game_website,
					'GAME_CREDITS'			=> $game_credits,
					'GAME_HISTORY'			=> $game_history,
					'GAME_LICENSE'			=> $game_license,
					'GAME_COPYRIGHTS'		=> $game_copyrights,
					'GAME_MEMBER_SLOTS'		=> $game_member_slots,
					'GAME_TICKS_LEFT'		=> $game_ticks_left,
					'GAME_ROUND'			=> $game_round,
					'GAME_ROUND_LENGTH'		=> $game_round_length,
					'GAME_BULLETIN'			=> $game_bulletin,
					
					'S_NEW_GAME'			=> $newgame,
					'S_TAB' 				=> $tab,
					'S_GAME_SELECTED'		=> $gameselected,
					'S_FORM_DATA'			=> $formdata,
					'S_GAME_ONLINE'			=> $game_online,
					'S_GAME_OPEN'			=> $game_open,
					'S_GAME_TIME_MGT' 		=> $game_time_mgt,
					'S_BULLETIN_SHOW'		=> $game_bulletin_show,
					)
				);
			break;		
		}		
		
				

	}
}
?>